import 'dart:async';

import 'package:fake_handwriting/Util.dart';
import 'package:fake_handwriting/generated/l10n.dart';
import 'package:fake_handwriting/http/http.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class Account extends StatefulWidget {
  const Account({Key? key}) : super(key: key);
  @override
  State<StatefulWidget> createState() => AccountPage();
}

class AccountPage extends State<Account> {
  final _nicknameController = TextEditingController();
  @override
  void dispose() {
    _nicknameController.dispose();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    init();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            SingleChildScrollView(
              child: Column(children: [
                Padding(
                  padding: const EdgeInsets.only(left: 10, right: 10, top: 30),
                  child: TextField(
                    maxLength: 10,
                    controller: _nicknameController,
                    cursorColor: Colors.blue,
                    decoration: InputDecoration(
                        enabledBorder: const OutlineInputBorder(
                          borderSide: BorderSide(color: Colors.blue),
                          borderRadius: BorderRadius.all(Radius.circular(10)),
                        ),
                        focusedBorder: const OutlineInputBorder(
                          borderSide: BorderSide(color: Colors.blue),
                          borderRadius: BorderRadius.all(Radius.circular(10)),
                        ),
                        labelText: S.of(context).inputNickname,
                        labelStyle: const TextStyle(color: Colors.blue),
                        hintText: S.of(context).inputNickname,
                        prefixIcon: const Icon(
                          Icons.label_important_outline_rounded,
                          color: Colors.blue,
                        ),
                        suffixIcon: InkWell(
                            onTap: () async {
                              if (_nicknameController.text.isNotEmpty) {
                                try {
                                  await Http.update_profile(_nicknameController.text);
                                  Util.toast(S.of(context).updateSuccessful);
                                  Navigator.pop(context,_nicknameController.text);
                                } catch (e) {
                                  Util.toast("${S.of(context).updateFailed}:$e");
                                }
                              } else {
                                Util.toast(S.of(context).inputNickname);
                              }
                            },
                            child: const Icon(
                              Icons.fingerprint,
                            ))),
                  ),
                ),
              ],),
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 10,vertical: 20),
              child: InkWell(
                onTap:(){
                  showCupertinoDialog(
                      context: context,
                      builder: (context) {
                        return CupertinoAlertDialog(
                          title: Text(S.of(context).confirmDelete),
                          content: Text(S.of(context).deleteAccountTips),
                          actions: <Widget>[
                            CupertinoDialogAction(child: Text(S.of(context).cancel),onPressed: (){
                              Navigator.pop(context);
                            },),
                            CupertinoDialogAction(child:Text(S.of(context).deleteAndLogout,style:const TextStyle(
                                color: Colors.red
                            ),),onPressed: () async {
                              if(await Http.delete_account()==S.of(context).deleteSucceed){
                                Util.toast(S.of(context).deleteSucceed);
                                await Util.removeToken();
                                Navigator.pop(context);
                              }
                              Navigator.pop(context);
                            },),
                          ],
                        );
                      });
                },
                child: Container(decoration: const BoxDecoration(
                    color: Colors.red,
                    borderRadius: BorderRadius.all(Radius.circular(10))
                ),child: Padding(padding: const EdgeInsets.symmetric(vertical: 20),child: Center(child: Text(S.of(context).deleteAccount,style: const TextStyle(
                    color: Colors.white
                ),)))),
              ),
            ),
          ],mainAxisAlignment: MainAxisAlignment.spaceBetween,
        ),
      ),
      appBar: AppBar(
        title:Text(S.of(context).myAccount),
      ),
    );
  }
  Future<void> init() async {
    var res=await Util.getTokenInfo();
  if(res==null){
   Navigator.pop(context);
  }else{
    _nicknameController.text=res["nickname"];
  }
    setState(() {});
  }
}
